package com.heima.search.v1.controller;

import com.heima.common.dtos.ResponseResult;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.search.pojo.ArticleDoc;
import com.heima.search.service.ApSearchHistroyService;
import com.heima.search.service.ApUserSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserSearchController {

    @Autowired
    private ApUserSearchService searchService;
    @Autowired
    private ApSearchHistroyService histroyService;

    /**
     * 搜索
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/article/search/search")
    public ResponseResult<List<ArticleDoc>> search(@RequestBody UserSearchDto dto){
        List<ArticleDoc> list = searchService.search(dto);
        return ResponseResult.ok(list);
    }

    /**
     * 历史搜索记录
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/history/load")
    public  ResponseResult<List<String>> associationSearch(@RequestBody UserSearchDto dto){
        List<String> history = histroyService.getHistory(UserThreadLocalUtils.getUserId(), dto.getEquipmentId(), dto.getSearchWords());;
        return ResponseResult.ok(history);
    }
}
